function h = plot_nanovis(obj,varargin)
%plot_nanovis plit Atoms


%Version 1.0.0
%global xcyl ycyl zcyl dirc n colorbond


%Read input
%p = inputParser;
%p.addParamValue('GraphicsQuality',10,@(x)x>=1 && mod(x,1)==0);
%p.addParamValue('Rendering','BallAndSticks',@(x)any(strcmpi(x,{'BallAndSticks','SpaceFill','Wireframe'})))
%p.parse(varargin{:});
%n=p.Results.GraphicsQuality;
%switch p.Results.Rendering
%    case 'BallAndSticks'
%     a=1;      
%     mb=2;
%    case 'SpaceFill'
%     a=2; 
%     mb=0;
%    case 'Wireframe'
%     a=0;
%     mb=1;
%end    

%Graphic Quality
n = 10;

%Rendering mode
a=1;      
mb=2;

[xcyl,ycyl,zcyl]=cylinder(1,n); %z-oriented
dirc=[1e-10 1e-10 10000']; %Create a vector with a z-direction
%%%

%General settings
colorbond=[0.5 0.5 0.5];
set(gcf,'DefaultSurfaceLineStyle','none')
set(gcf,'DefaultSurfaceFaceColor',colorbond)
n=10;
%Check the rendering
%Read DataBase%%%%%%%%%%%%%%%%%%%%%%%%%
species = obj.specie;


ns=numel(species);
SpecDes=zeros(ns,6);
DB=importdata('DataBase.dat');
for ks=1:numel(species)
   index = strmatch(species(ks),DB.textdata(:,2),'exact');
   SpecDes(ks,:) = DB.data(index,1:6);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
%Spheres
natom = obj.natoms;
kb = 0;
if ~(a==0)
[Sx Sy Sz]=sphere(n); 
for ka=1:natom
  ks=obj.Atoms(ka).Specie;
  
  color=SpecDes(ks,4:6);
  Ray(ks)=a*SpecDes(ks,1)*0.7;
  kb = kb +1;
  h(kb) = surface(Ray(ks)*Sx + obj.Atoms(ka).Position(1),Ray(ks)*Sy + obj.Atoms(ka).Position(2),Ray(ks)*Sz + obj.Atoms(ka).Position(3),'FaceColor',color,'LineStyle','none');
end

end

%%%%%%%%%%%%%%%%%%%%%%%%%%%

%Bonds

   %Compute Valences
   for ka=1:natom
      ks=obj.Atoms(ka).Specie;
      valence(ka)=round(SpecDes(ks,3));   
   end
 
   bn=zeros(1,natom); %Temp Valences
   for k1=1:natom-1
       if bn(k1)<valence(k1)
           for k2=k1+1:natom
               if bn(k2)<valence(k2)
                  r=norm(obj.Atoms(k1).Position-obj.Atoms(k2).Position); 
                  if (r <= SpecDes(obj.Atoms(k1).Specie,2)+SpecDes(obj.Atoms(k2).Specie,2))
                     bn(k1)=bn(k1)+1;
                     bn(k2)=bn(k2)+1;
                     
                           Radius=min(Ray(obj.Atoms(k1).Specie),Ray(obj.Atoms(k2).Specie))/2; 
                           kb = kb +1;
                           
                           
                           %Draw bond 
                           %XData
                           a=xcyl*Radius;
                           %YData
                           b=ycyl*Radius;
                           %ZData
                           c=zcyl;
                           %first atom position
                           pc(1,1:3)=obj.Atoms(k1).Position;
                           %second atom position
                           pc(2,1:3)=obj.Atoms(k2).Position;
                           %Bond creation
                           [m,I]=sort(pc(:,3));
                           a1=I(1);
                           b1=I(2);
                           h(kb)=surface(a+pc(a1,1),b+pc(a1,2),r*c+pc(a1,3),'FaceColor',colorbond,'Tag','str');
                           a=pc(b1,:)-pc(a1,:);
                           b=dirc;
                           cr(1) = b(3)*a(2) - b(2)*a(3);
                           cr(2) = b(1)*a(3) - b(3)*a(1);
                           cr(3) = b(2)*a(1) - b(1)*a(2);
                           dir=pc(a1,:)+cr;
                           alfa=(180/pi)*(atan2(pc(b1,3)-pc(a1,3),((pc(b1,1)-pc(a1,1))^2+(pc(b1,2)-pc(a1,2))^2)^(1/2))-pi/2);                               
                           rotate(h,dir,alfa,pc(a1,:)+1e-6)
                           
          
                         
                     
               end  
           end
       end  
   end
   

end
%Graphics setting
%lighting gouraud
%lighiting phong
%light('Position',[1 0 0],'Style','infinite');
%set(gca,'DataAspectRatio',[ 1 1 1 ])
%set(gca,'PlotBoxAspectRatio',[ 1 1 1 ])
%set(gca,'CameraViewAngleMode','manual')
%set(gcf,'Color',[0.7 0.7 0.8])
%set(gcf,'Name','Atomistic Visualizer')
%camproj('perspective')
%set(gca,'XTick',[])
%set(gca,'YTick',[])
%set(gca,'ZTick',[])
%axis off
%hold off

 
end
